package com.superm.controller;

import com.superm.entity.Goods;
import com.superm.entity.RepostoryType;
import com.superm.entity.Stock;
import com.superm.mappers.StockMapper;
import com.superm.service.BrandService;
import com.superm.service.GoodsService;
import com.superm.service.RepostoryService;
import com.superm.service.StockService;
import com.superm.service.impl.SupplierServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(path = "/inventory")
public class InventoryController {
    @Autowired
    private StockService stockService;
    @Autowired//仓库
    private RepostoryService repostoryService;
    @Autowired//供应商
    private SupplierServiceImpl supplierService;
    @Autowired//商品
    private GoodsService goodsService;
    @RequestMapping(path = "index")
    public ModelAndView index(){
        ModelAndView ma = new ModelAndView();
        ma.addObject("reps",repostoryService.allRepostory());
        ma.addObject("supp",supplierService.allSup());
        ma.setViewName("inventory/allInventory");
        return ma;
    }
    @RequestMapping(path = "/allInventory")
    @ResponseBody
    public Map<String,Object> allInventory(Stock inv, @RequestParam("page") Integer pageNo, @RequestParam("limit")Integer pageSize){
        Map<String,Object> map =new HashMap<>();
        List<Stock> invs = stockService.allstock(inv,pageNo,pageSize);
        //获取总条数
        Integer count = stockService.selectCount(inv);
        System.out.println(count);
        map.put("code",0);
        map.put("count",count);
        map.put("data",invs);
        return map;
    }
    @RequestMapping(path = "/delInventory")
    @ResponseBody
    public Map<String,Object> delRole(@RequestParam("invs")String invs){
        List<String> idList = Arrays.asList(invs.split(","));
        Map<String,Object> map = new HashMap<>();
        if(stockService.delInventorys(idList)>0){
            map.put("success",true);
            map.put("msg","删除成功!");
        }else{
            map.put("success",false);
            map.put("msg","删除失败!");
        }
        return map;
    }
    @GetMapping(path = "/upInventory")
    public ModelAndView  upInventory(Integer stockId){
        ModelAndView ma = new ModelAndView();
        Stock st =  stockService.queryId(stockId);
        ma.addObject("reps",repostoryService.allRepostory());
        ma.addObject("supp",supplierService.allSup());
        ma.addObject("st",st);
        ma.setViewName("inventory/upInventory");
        return ma;
    }
    @PostMapping(path = "/upInventory")
    @ResponseBody
    public Map<String,Object> upInventory(Stock stock,Integer capacity){
        return stockService.upInventory(stock,capacity);
    }
    @PostMapping(path = "/upJaoyan")//校验 输入的库存数量是否大于仓库的容量
    @ResponseBody
    public Map<String,Object> upJiaoyan(Stock stock,Integer capacity/*,Integer Yreponseid,Integer YstockCount,*/){
        return stockService.upJaoyan(stock,capacity/*,Yreponseid,YstockCount,capacity*/);
    }
    @GetMapping(path = "addInventory")
    public ModelAndView addInventory(){
        ModelAndView ma = new ModelAndView();
        ma.addObject("reps",repostoryService.allRepostory());
        ma.addObject("supp",supplierService.allSup());
        ma.addObject("goods",goodsService.allGoodSs());
        ma.setViewName("inventory/addInventory");
        return ma;
    }

    @PostMapping(path = "addInventory")
    @ResponseBody
    public Map<String,Object> addInventory(Stock stock){
        return stockService.addInventory(stock);
    }
    @PostMapping(path = "/addJiaoyan")//校验 输入的库存数量是否大于仓库的容量
    @ResponseBody
    public Map<String,Object> addJiaoyan(Stock stock){
        System.out.println(stock);
        return stockService.addJiaoyan(stock);
    }
}
